package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.pojo.User;
import com.itheima.common.R;
import com.itheima.service.UserService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/5 13:43
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 将验证码存储到redis缓存中，设置过期时间
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/sendMsg")
    public R login(@RequestBody User user, HttpSession session){
        String phone = user.getPhone();
        // 获取随机验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        log.info("验证码为"+code);
        // 通过阿里云发送短信
//        SMSUtils.sendMessage("黑马程序员", "SMS_171354585", String.valueOf(integer),user.getPhone() );
//       session.setAttribute("userCode", String.valueOf(code));

       // 将手机号作为键，验证码作为值存储到redis里面，并且设置redis过期时间
       redisTemplate.opsForValue().set(phone, code, 60, TimeUnit.SECONDS);

        return R.success("手机验证码输入成功！");
    }

    /**
     * 验证用户输入的验证码和手机号是否正确
     *
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Map<String, String> map, HttpSession session){
        String code = map.get("code");
        String phone = map.get("phone");

//        String userCode = (String) session.getAttribute("userCode");
        // 从redis中取出判断，过期时间是一分钟
        String userCode = (String) redisTemplate.opsForValue().get(phone);
        if(userCode == null || !userCode.equals(code)){
            return R.error("验证码或者手机号码错误！");
        }
        LambdaQueryWrapper<User> querryWrapper = new LambdaQueryWrapper<>();
        querryWrapper.eq(User::getPhone, phone);
        User user = userService.getOne(querryWrapper);
        // 如果该用户是第一次登录，就将手机号存入到表中
        if(user == null){
            user = new User();
            user.setPhone(phone);
            userService.save(user);
        }
//        session.removeAttribute("userCode");
        // 将用户id存到session中判断
        session.setAttribute("user", user.getId());
        redisTemplate.delete(phone);
        return R.success("登录成功！");
    }

    @PostMapping("/loginout")
    public R loginout(HttpSession session){
        session.removeAttribute("user");
        return R.success("退出成功");
    }

}
